package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;

import com.mall.commons.tool.utils.TradeNoUtils;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.dto.CreateOrderResponse;
import com.mall.order.dto.OrderListRequest;
import com.mall.order.dto.OrderListResponse;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;

import com.mall.order.OrderQueryService;
import com.mall.order.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author zhizhuke
 * @version 1.0
 * @description:
 * @date 2022-01-19 20-37
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(timeout = 3000, retries = 0, check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 3000, retries = 0, check = false)
    OrderQueryService orderQueryService;


    /**
     * 获取订单详情
     *
     * @return
     */

//    @GetMapping("/order/{id}")
//    public ResponseData orderDetail(@PathVariable("id") String id) {
//        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
//        orderDetailRequest.setOrderId(id);
//
//        OrderDetailResponse orderDetailResponse = orderQueryService.selectOrderDetail(orderDetailRequest);
//        if (orderDetailResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
//            return new ResponseUtil().setData(orderDetailResponse);
//        }
//        return new ResponseUtil().setErrorMsg(orderDetailResponse.getMsg());
//    }
    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String id) {
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);

        OrderDetailResponseMy orderDetailResponseMy = orderQueryService.selectOrderDetail(orderDetailRequest);

        if (orderDetailResponseMy.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(orderDetailResponseMy);
        }
        return new ResponseUtil().setErrorMsg(orderDetailResponseMy.getMsg());
    }

    /**
     * 取消订单
     *
     * @return
     */
    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {

        cancelOrderRequest.setOrderId(cancelOrderRequest.getOrderId());

        CancelOrderResponse cancelOrderResponse =  orderQueryService.cancelOrder(cancelOrderRequest);
        if (cancelOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(cancelOrderResponse);
        }
        return new ResponseUtil().setErrorMsg(cancelOrderResponse.getMsg());
    }
    /**
     * 删除订单
     *
     * @return
     */
    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {

        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);

        DeleteOrderResponse deleteOrderResponse= orderQueryService.deleteOrder(deleteOrderRequest);

        if (deleteOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(deleteOrderResponse);
        }
        return new ResponseUtil().setErrorMsg(deleteOrderResponse.getMsg());
    }



    /**
     * @description:
     * @param: 创建订单
     * @return:
     * @author zhizhuke
     * @date: 2022-01-20 16:49
     */
    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest orderRequest, HttpServletRequest httpRequest) {
        String userInfo = (String) httpRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long uid = Long.parseLong(jsonObject.getString("uid"));
        orderRequest.setUserId(uid);
        orderRequest.setUniqueKey(TradeNoUtils.generateTradeNo());//利用单号生成器，生成全局唯一的id

        CreateOrderResponse orderResponse = orderCoreService.createOrder(orderRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(orderResponse.getCode())) {
            return new ResponseUtil().setData(orderResponse.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(orderResponse.getMsg());
    }

    /**
     * @description:
     * @param: 查询订单
     * @return:
     * @author zhizhuke
     * @date: 2022-01-20 16:52
     */
    @GetMapping("/order")
    public ResponseData order(OrderListRequest listRequest, HttpServletRequest httpRequest) {
        String userInfo = (String) httpRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long userId = Long.parseLong(jsonObject.getString("uid"));
        listRequest.setUserId(userId);

        OrderListResponse listResponse = orderQueryService.listOrder(listRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(listResponse.getCode())) {
            return new ResponseUtil().setData(listResponse.getOrderListDto());
        }
        return new ResponseUtil().setErrorMsg(listResponse.getMsg());
    }
}

